<template>
  <el-dialog
    :title="projectName"
    :close-on-click-modal="false"
    :visible.sync="visible">
     <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="Web端" name="web">
          <div style="width:100%;height:400px;border: 1px solid #cccccc;padding:20px;">
              <el-form ref="dataForm"  label-width="130px">
                <el-form-item label="所在服务器">
                    <el-select  v-model="dataForm.serverId" placeholder="请选择">

                       <el-option v-for="( server,index) in servers" :label="server.name+'  【'+server.ipAddr+'】'" :value="server.id"></el-option>
                     </el-select>
                </el-form-item>

                  <el-form-item label="所属服务类型">
                      <el-select v-model="dataForm.webServerType" placeholder="请选择">
                         <el-option label="无" value=""></el-option>
                         <el-option label="Nginx" value="nginx"></el-option>
                       </el-select>
                  </el-form-item>

                  <el-form-item label="服务根路径">
                      <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
                  <el-form-item label="配置文件路径">
                    <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
                  <el-form-item label="部署路径">
                    <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
              </el-form>
          </div>
          <div style="margin-top: 20px;text-align: right;">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane label="服务器端" name="server">
          <div style="width:100%;height:400px;border: 1px solid #cccccc;padding:20px;">
              <el-form ref="dataForm"  label-width="130px">

                <el-form-item label="所在服务器">
                    <el-select  v-model="dataForm.serverId" placeholder="请选择">

                       <el-option v-for="( server,index) in servers" :label="server.name+'  【'+server.ipAddr+'】'" :value="server.id"></el-option>
                     </el-select>
                </el-form-item>

                  <el-form-item label="所属服务类型">
                      <el-select v-model="dataForm.webServerType" placeholder="请选择">
                         <el-option label="无" value=""></el-option>
                         <el-option label="springboot" value="springboot"></el-option>
                       </el-select>
                  </el-form-item>

                  <el-form-item label="服务根路径">
                      <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
              </el-form>
          </div>
          <div style="margin-top: 20px;text-align: right;">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane label="数据库" name="database">
          <div style="width:100%;height:400px;border: 1px solid #cccccc;padding:20px;">
              <el-form ref="dataForm"  label-width="130px">

                <el-form-item label="所在服务器">
                    <el-select  v-model="dataForm.serverId" placeholder="请选择">

                       <el-option v-for="( server,index) in servers" :label="server.name+'  【'+server.ipAddr+'】'" :value="server.id"></el-option>
                     </el-select>
                </el-form-item>

                  <el-form-item label="所属服务类型">
                      <el-select v-model="dataForm.webServerType" placeholder="请选择">
                         <el-option label="无" value=""></el-option>
                         <el-option label="Mysql" value="mysql"></el-option>
                       </el-select>
                  </el-form-item>

                  <el-form-item label="服务根路径">
                      <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
              </el-form>
          </div>
          <div style="margin-top: 20px;text-align: right;">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane label="缓存" name="cache">
          <div style="width:100%;height:400px;border: 1px solid #cccccc;padding:20px;">
              <el-form ref="dataForm"  label-width="130px">

                <el-form-item label="所在服务器">
                    <el-select  v-model="dataForm.serverId" placeholder="请选择">

                       <el-option v-for="( server,index) in servers" :label="server.name+'  【'+server.ipAddr+'】'" :value="server.id"></el-option>
                     </el-select>
                </el-form-item>

                  <el-form-item label="所属服务类型">
                      <el-select v-model="dataForm.webServerType" placeholder="请选择">
                         <el-option label="无" value=""></el-option>
                         <el-option label="Redis" value="redis"></el-option>
                       </el-select>
                  </el-form-item>

                  <el-form-item label="服务根路径">
                      <el-input v-model="dataForm.name"></el-input>
                  </el-form-item>
              </el-form>
          </div>
          <div style="margin-top: 20px;text-align: right;">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </div>
          </el-tab-pane>
          <el-tab-pane label="数据仓库" name="datawarehouse ">
            <div style="width:100%;height:400px;border: 1px solid #cccccc;padding:20px;">
                <el-form ref="dataForm"  label-width="130px">

                  <el-form-item label="所在服务器">
                      <el-select  v-model="dataForm.serverId" placeholder="请选择">

                         <el-option v-for="( server,index) in servers" :label="server.name+'  【'+server.ipAddr+'】'" :value="server.id"></el-option>
                       </el-select>
                  </el-form-item>

                    <el-form-item label="所属服务类型">
                        <el-select v-model="dataForm.webServerType" placeholder="请选择">
                           <el-option label="无" value=""></el-option>
                           <el-option label="MongoDB" value="mongodb"></el-option>
                         </el-select>
                    </el-form-item>

                    <el-form-item label="服务根路径">
                        <el-input v-model="dataForm.name"></el-input>
                    </el-form-item>
                </el-form>
            </div>
          <div style="margin-top: 20px;text-align: right;">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </div>
        </el-tab-pane>
      </el-tabs>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      return {
        visible: false,
        projectId:"",
        activeName: 'web',
        servers:[],
        dataForm: {
          serverId:'',
          webServerType:""
        },
        dataRule: {
          name: [
            { required: true, message: '服务器名称不能为空', trigger: 'blur' }
          ],

          ipAddr: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      init (object) {
        this.visible = true
        this.projectId = object.id || 0
        this.projectName = object.name
        this.$http({
          url: this.$http.adornUrl(`/systemconfig/servers/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.servers = data.data
          }
        })
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/systemconfig/servers/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.server.name
                this.dataForm.ipAddr = data.server.ipAddr
                this.dataForm.email = data.server.email
                this.dataForm.configuration = data.server.configuration
                this.dataForm.status = data.server.status
              }
            })
          }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/systemconfig/servers/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'ipAddr': this.dataForm.ipAddr,
                'configuration': this.dataForm.configuration,
                'status': this.dataForm.status,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
